package com.heyqing.ems.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heyqing.ems.domain.BossInfo;
import com.heyqing.ems.domain.EmployeeInfo;
import com.heyqing.ems.mapper.EmployeeInfoMapper;
import com.heyqing.ems.mapper.ManagerInfoMapper;
import com.heyqing.ems.po.QueryPo;
import com.heyqing.ems.service.EmployeeService;
import com.heyqing.ems.vo.EmployeeVo;
import com.heyqing.ems.vo.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * ClassName:EmployeeServiceImpl
 * Package:com.heyqing.ems.service.impl
 * Description:
 *
 * @Date:2024/6/26
 * @Author:Heyqing
 */
@Service
@RequiredArgsConstructor
public class EmployeeServiceImpl extends ServiceImpl<EmployeeInfoMapper, EmployeeInfo> implements EmployeeService {

    private final ManagerInfoMapper managerInfoMapper;
    @Override
    public Result getList(QueryPo queryPo) {
        if (Objects.isNull(queryPo)){
            return Result.fail();
        }
        Page<EmployeeInfo> resPage = lambdaQuery()
                .like(EmployeeInfo::getName, queryPo.getQuery())
                .page(new Page<EmployeeInfo>(queryPo.getPageNum(), queryPo.getPageSize()));
        if (Objects.isNull(resPage) || Objects.isNull(resPage.getRecords()) || Objects.isNull(resPage.getTotal())){
            return Result.fail();
        }
        Map<String,Object> map = new HashMap<>();
        List<EmployeeInfo> records = resPage.getRecords();
        List<EmployeeVo> res = copy2Vo(records);
        map.put("list",res);
        map.put("total",resPage.getTotal());
        return Result.success(map);
    }

    /********************************************private*****************************************/

    private EmployeeVo copy(EmployeeInfo info){
        EmployeeVo employee = new EmployeeVo();
        BeanUtil.copyProperties(info,employee);
        String name = managerInfoMapper.selectById(info.getManagerId()).getName();
        employee.setManager(name);
        return employee;
    }
    private List<EmployeeVo> copy2Vo(List<EmployeeInfo> records) {
        List<EmployeeVo> res = new ArrayList<>();
        for (EmployeeInfo record : records) {
            res.add(copy(record));
        }
        return res;
    }
}
